package com.yhny.lykj.yuhannongye.app;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.longsichao.lscframe.net.Api;
import com.longsichao.lscframe.view.LSCToast;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.tools.LCLog;

import java.util.LinkedList;
import java.util.List;

public abstract class BaseTabPagerActivity<M> extends BaseActivity {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private List<M> dataList = new LinkedList<>();
    TabFragmentAdapter tabFragmentAdapter;
    private int currentPage = 1;
    private boolean isFirstLoad = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResource());

        onSetContentViewAfter();
        if (isLoadDataInFirst()) {
            loadData(1);
        }
        preloadData();
    }
    private void loadData(int page) {
        currentPage = page;
        if (isFirstLoad) {
            isFirstLoad = false;
        }
        DataManager.getData(MODEL_PROCESS_0, getApi(currentPage), this);
    }
    protected abstract Api getApi(int page);
    protected List<M> getDataList() {
        return dataList;
    }
    protected void onSetContentViewAfter() {
    }

    protected void preloadData() {
    }

    @LayoutRes
    protected int getLayoutResource() {
        return R.layout.activity_tab_pager;
    }

    protected abstract List<Fragment> getFragmentList(List<M> dataList);

    protected abstract List<String> getTitleList(List<M> dataList);

    private void initTabPagerView(List<M> dataList) {

        List<Fragment> fragmentList = getFragmentList(dataList);
        List<String> titleList = getTitleList(dataList);
        tabFragmentAdapter = new TabFragmentAdapter(getSupportFragmentManager(), fragmentList, titleList);

        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(tabFragmentAdapter);
        viewPager.setOffscreenPageLimit(3);

        tabLayout = (TabLayout) findViewById(R.id.tab);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        for (String title : titleList) {
            tabLayout.addTab(tabLayout.newTab().setText(title));
        }
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(tabFragmentAdapter);


        TabLayout.Tab tab = tabLayout.getTabAt(selectPage());
        if (tab != null) {
            tab.select();
        }
    }
    protected boolean isLoadDataInFirst() {
        return true;
    }
    protected abstract List<M> getModelDataList(LSCModel model);
    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)||model==null) {
//            LSCToast.show(this,"请检查网络！");
            dismissProgressDialog();
            return false;
        }
        switch (caseId) {
            case MODEL_PROCESS_0:
                List<M> modelDataList = getModelDataList(model);
                if (modelDataList == null || modelDataList.isEmpty()) {
                    LCLog.e("onModel dataList=null");
                } else {
                    dataList.clear();
                    dataList.addAll(modelDataList);
                    onSetContentViewAfter();
                    initTabPagerView(dataList);
                    tabFragmentAdapter.notifyDataSetChanged();
                }

                dismissProgressDialog();
                return true;
        }
        return false;
    }
    protected int selectPage() {
        return 0;
    }

    protected void resizeViewInViewPager(ViewPager viewPager) {
    }
}
